package com.jt.www.remote;

import com.jt.www.domain.dto.OfflineProductDTO;
import com.jt.www.model.remote.product.AreaQuery;
import com.jt.www.model.remote.product.ProdResultVo;
import com.jt.www.model.remote.product.RiskInfoVo;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.woxuebao.Risk.RiskDTO;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//@FeignClient(value = "prod-svc",url = "${remote.pro_svc_url}")
@FeignClient(value = "prod-svc", url = "${remote.pro_svc_url}", configuration = FeignConfiguration.class, fallbackFactory = ProductClient.FeignClientFallbackFactory.class)
public interface ProductClient {

    //    查询产品列表(产品编码与产品名) @PostMapping(value = "/product_channel/{channel}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = "application/json;charset=UTF-8")
    @PostMapping("/product_channel/{channel}")
    ResponseEntity<GenericListResponse<ProdResultVo>> searchProductList(@PathVariable("channel") String channel, @RequestBody AreaQuery jsonstr) throws Exception;

    @GetMapping(value = "/productdetailsvc", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    String productdetailsvc(@RequestParam("id") String id, @RequestParam("channelNo") String channelNo) throws Exception;

    /**
     * 通过产品编码查询险种详情
     *
     * @param id
     * @param channelNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/product/{id}/risk", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericListResponse<RiskInfoVo> searchRiskByProductCode(@PathVariable("id") String id,
                                                            @RequestParam(value = "channelNo", defaultValue = "edu") String channelNo) throws Exception;

    /**
     * 通过产品编码查询险种详情及责任限额
     *
     * @param riskCode  产品编码
     * @param channelNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/product/{riskCode}/risk", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericListResponse<RiskDTO> searchRiskDtoByProductCode(@PathVariable("riskCode") String riskCode,
                                                            @RequestParam(value = "channelNo", defaultValue = "edu") String channelNo) throws Exception;

    /**
     * 通过渠道编码和销售区域查询产品列表(产品编码与产品名)
     *
     * @param json
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/api/v3/product_channel/edu", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    GenericListResponse<OfflineProductDTO> productChannel(
            @RequestBody Map<String, Object> map);


    @Component
    class FeignClientFallbackFactory implements FallbackFactory<ProductClient> {
        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public ProductClient create(Throwable cause) {
            return new ProductClient() {
                @Override
                public ResponseEntity<GenericListResponse<ProdResultVo>> searchProductList(String channel, AreaQuery jsonstr) throws Exception {
                    log.error("远程获取产品列表失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public String productdetailsvc(String id, String channelNo) throws Exception {
                    log.error("远程获取产品详情失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<RiskInfoVo> searchRiskByProductCode(String id, String channelNo) throws Exception {
                    log.error("远程通过产品编码查询险种详情失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<RiskDTO> searchRiskDtoByProductCode(String id, String channelNo) throws Exception {
                    log.error("远程通过产品编码查询险种详情及责任限额失败,fallback reason:", cause);
                    return null;
                }

                @Override
                public GenericListResponse<OfflineProductDTO> productChannel(Map<String, Object> map) {
                    log.error("查询线下录入的产品列表,fallback reason:", cause);
                    return null;
                }
            };
        }
    }
}
